/**
 * 警告和通知模块
 * 用于显示各种类型的提示信息
 */

const Alerts = {
    /**
     * 显示提示信息
     * @param {string} message - 消息内容
     * @param {string} type - 消息类型 (success, info, warning, danger)
     * @param {number} duration - 显示持续时间(毫秒)，默认3000ms
     */
    show: function(message, type = 'info', duration = 3000) {
        // 创建弹出提示
        const alertDiv = document.createElement('div');
        alertDiv.className = `alert alert-${type} alert-dismissible`;
        alertDiv.style.position = 'fixed';
        alertDiv.style.top = '20px';
        alertDiv.style.right = '20px';
        alertDiv.style.zIndex = '9999';
        alertDiv.style.maxWidth = '400px';
        alertDiv.style.boxShadow = '0 4px 8px rgba(0,0,0,0.1)';
        alertDiv.style.transition = 'all 0.3s ease';
        alertDiv.style.opacity = '0';
        alertDiv.style.transform = 'translateY(-20px)';
        
        alertDiv.innerHTML = `
            ${message}
            <button type="button" class="close" onclick="this.parentElement.remove();">&times;</button>
        `;
        
        document.body.appendChild(alertDiv);
        
        // 添加淡入效果
        setTimeout(() => {
            alertDiv.style.opacity = '1';
            alertDiv.style.transform = 'translateY(0)';
        }, 10);
        
        // 设置持续时间后自动消失
        if (duration > 0) {
            setTimeout(() => {
                alertDiv.style.opacity = '0';
                alertDiv.style.transform = 'translateY(-20px)';
                
                setTimeout(() => {
                    alertDiv.remove();
                }, 300);
            }, duration);
        }
        
        return alertDiv;
    },
    
    /**
     * 显示成功消息
     * @param {string} message - 消息内容
     * @param {number} duration - 显示持续时间(毫秒)
     */
    success: function(message, duration = 3000) {
        return this.show(message, 'success', duration);
    },
    
    /**
     * 显示信息消息
     * @param {string} message - 消息内容
     * @param {number} duration - 显示持续时间(毫秒)
     */
    info: function(message, duration = 3000) {
        return this.show(message, 'info', duration);
    },
    
    /**
     * 显示警告消息
     * @param {string} message - 消息内容
     * @param {number} duration - 显示持续时间(毫秒)
     */
    warning: function(message, duration = 3000) {
        return this.show(message, 'warning', duration);
    },
    
    /**
     * 显示错误消息
     * @param {string} message - 消息内容
     * @param {number} duration - 显示持续时间(毫秒)
     */
    error: function(message, duration = 3000) {
        return this.show(message, 'danger', duration);
    },
    
    /**
     * 显示确认对话框
     * @param {string} message - 消息内容
     * @param {string} title - 标题
     * @param {Function} onConfirm - 确认回调
     * @param {Function} onCancel - 取消回调
     */
    confirm: function(message, title = '确认', onConfirm = null, onCancel = null) {
        // 创建确认对话框
        const modal = document.createElement('div');
        modal.className = 'modal';
        modal.style.display = 'block';
        
        modal.innerHTML = `
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">${title}</h5>
                        <button type="button" class="close" data-action="cancel">&times;</button>
                    </div>
                    <div class="modal-body">
                        <p>${message}</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-action="cancel">取消</button>
                        <button type="button" class="btn btn-primary" data-action="confirm">确认</button>
                    </div>
                </div>
            </div>
        `;
        
        document.body.appendChild(modal);
        
        // 添加按钮事件
        const buttons = modal.querySelectorAll('button');
        buttons.forEach(button => {
            button.addEventListener('click', function() {
                const action = this.dataset.action;
                
                if (action === 'confirm' && typeof onConfirm === 'function') {
                    onConfirm();
                } else if (action === 'cancel' && typeof onCancel === 'function') {
                    onCancel();
                }
                
                modal.remove();
            });
        });
        
        return modal;
    }
}; 